/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxsodetexptapi.p                                 */
/* PROCEDURE TYPE        : Export Data API Export Process                   */
/* DESCRIPTION           : So export Process                                */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                                27-Jul-24  */
/****************************************************************************/ 
/****************************************************************************/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}
{xxjson.i}
{xxcimload.i}

&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


DEFINE VARIABLE li_access           AS    INTEGER                       NO-UNDO.
define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.
define variable lvi_row_id                   as decimal     no-undo.
define variable v_import                     as logical     init   "no"      no-undo.
define variable v_site                       like si_site   no-undo.

define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical initial yes      no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.

define variable v_entity              like si_entity              no-undo.
define variable v_ee                  as logic                    no-undo.

define variable vi_filecount          as integer                  no-undo.
define variable vc_filename           as character                no-undo.
define variable vc_topath             as character                no-undo.
define variable v_runproagram        as character                no-undo.
define variable vc_traceid            as character                no-undo.


 define temp-table ttSearchCondition no-undo
        field customer_from         like debtor.debtorCode
        field customer_to           like debtor.debtorCode
        field sonbr_from            like so_mstr.so_nbr
        field sonbr_to              like so_mstr.so_nbr
        field so_type               as   character
        field so_sched              like so_mstr.so_sched
        field so_stat               as   character
		field site_from             as character
        field site_to               as character
        .


 define temp-table ttsodet
 
 fields so_nbr            like    so_mstr.so_nbr                   
 fields so_cust           like    so_mstr.so_cust                
 fields so_ship           like    so_mstr.so_ship                
 fields so_ord_date       like    so_mstr.so_ord_date            
 fields so_req_date       like    so_mstr.so_req_date            
 fields so_due_date       like    so_mstr.so_due_date            
 fields so_rmks           like    so_mstr.so_rmks                
 fields so_cr_terms       like    so_mstr.so_cr_terms            
 fields so_fob            like    so_mstr.so_fob                 
 fields so_po             like    so_mstr.so_po                  
 fields so_shipvia        like    so_mstr.so_shipvia             
 fields so_pr_list        like    so_mstr.so_pr_list             
 fields so_xslspsn        like    so_mstr.so_xslspsn             
 fields so_stat           like    so_mstr.so_stat                
 fields so_invoiced       like    so_mstr.so_invoiced            
 fields so_ar_acct        like    so_mstr.so_ar_acct             
 fields so_ar_cc          like    so_mstr.so_ar_cc               
 fields so_inv_date       like    so_mstr.so_inv_date            
 fields so_ship_date      like    so_mstr.so_ship_date           
 fields so_taxable        like    so_mstr.so_taxable             
 fields so_curr           like    so_mstr.so_curr                
 fields so_ex_rate        like    so_mstr.so_ex_rate             
 fields so_lang           like    so_mstr.so_lang                
 fields so_type           like    so_mstr.so_type                
 fields so_site           like    so_mstr.so_site                
 fields so_taxc           like    so_mstr.so_taxc                
 fields so_project        like    so_mstr.so_project             
 fields so_channel        like    so_mstr.so_channel             
 fields so_fix_rate       like    so_mstr.so_fix_rate            
 fields so_bill           like    so_mstr.so_bill                
 fields so_tax_env        like    so_mstr.so_tax_env             
 fields so_sched          like    so_mstr.so_sched               
 fields so_pr_list2       like    so_mstr.so_pr_list2            
 fields so_tax_usage      like    so_mstr.so_tax_usage           
 fields so_fix_pr         like    so_mstr.so_fix_pr              
 fields so_ex_rate2       like    so_mstr.so_ex_rate2            
 fields so_ex_ratetype    like    so_mstr.so_ex_ratetype         
 fields so_consignment    like    so_mstr.so_consignment         
 fields so_daybookset     like    so_mstr.so_daybookset                         
 fields sod_nbr           like    sod_det.sod_nbr                
 fields sod_due_date      like    sod_det.sod_due_date           
 fields sod_per_date      like    sod_det.sod_per_date           
 fields sod_req_date      like    sod_det.sod_req_date           
 fields sod_line          like    sod_det.sod_line               
 fields sod_part          like    sod_det.sod_part               
 fields sod_qty_ord       like    sod_det.sod_qty_ord            
 fields sod_qty_all       like    sod_det.sod_qty_all            
 fields sod_qty_pick      like    sod_det.sod_qty_pick           
 fields sod_qty_ship      like    sod_det.sod_qty_ship           
 fields sod_qty_inv       like    sod_det.sod_qty_inv            
 fields sod_loc           like    sod_det.sod_loc                
 fields sod_type          like    sod_det.sod_type               
 fields sod_price         like    sod_det.sod_price              
 fields sod_std_cost      like    sod_det.sod_std_cost           
 fields sod_qty_chg       like    sod_det.sod_qty_chg            
 fields sod_bo_chg        like    sod_det.sod_bo_chg             
 fields sod_acct          like    sod_det.sod_acct               
 fields sod_taxable       like    sod_det.sod_taxable            
 fields sod_desc          like    sod_det.sod_desc               
 fields sod_um            like    sod_det.sod_um                 
 fields sod_cc            like    sod_det.sod_cc                 
 fields sod_um_conv       like    sod_det.sod_um_conv            
 fields sod_project       like    sod_det.sod_project            
 fields sod_custpart      like    sod_det.sod_custpart           
 fields sod_status        like    sod_det.sod_status             
 fields sod_list_pr       like    sod_det.sod_list_pr            
 fields sod_taxc          like    sod_det.sod_taxc               
 fields sod_inv_nbr       like    sod_det.sod_inv_nbr            
 fields sod_site          like    sod_det.sod_site               
 fields sod_prodline      like    sod_det.sod_prodline           
 fields sod_tax_in        like    sod_det.sod_tax_in             
 fields sod_confirm       like    sod_det.sod_confirm            
 fields sod_ord_mult      like    sod_det.sod_ord_mult           
 fields sod_pkg_code      like    sod_det.sod_pkg_code           
 fields sod_sched         like    sod_det.sod_sched              
 fields sod_sch_data      like    sod_det.sod_sch_data           
 fields sod_sch_mrp       like    sod_det.sod_sch_mrp            
 fields sod_translt_days  like    sod_det.sod_translt_days       
 fields sod_fsm_type      like    sod_det.sod_fsm_type           
 fields sod_conrep        like    sod_det.sod_conrep             
 fields sod_start_eff     as      date    
 fields sod_end_eff       as      date    
 fields sod_dock          like    sod_det.sod_dock               
 fields sod_pr_list       like    sod_det.sod_pr_list            
 fields sod_translt_hrs   like    sod_det.sod_translt_hrs        
 fields sod_out_po        like    sod_det.sod_out_po             
 fields sod_raw_days      like    sod_det.sod_raw_days           
 fields sod_fab_days      like    sod_det.sod_fab_days           
 fields sod_tax_usage     like    sod_det.sod_tax_usage          
 fields sod_rbkt_days     like    sod_det.sod_rbkt_days          
 fields sod_rbkt_weeks    like    sod_det.sod_rbkt_weeks         
 fields sod_rbkt_mths     like    sod_det.sod_rbkt_mths          
 fields sod_auto_ins      like    sod_det.sod_auto_ins           
 fields sod_tax_env       like    sod_det.sod_tax_env            
 fields sod_sub           like    sod_det.sod_sub                
 fields sod_qty_ivcd      like    sod_det.sod_qty_ivcd           
 fields sod_custref       like    sod_det.sod_custref            
 fields sod_consignment   like    sod_det.sod_consignment        
 fields sod_scx_po        like    scx_ref.scx_po       
 fields sod_scx_modelyr   like    scx_ref.scx_modelyr       
 fields sod_msg           as      character      
         
 index idx1 sod_nbr sod_line.
 
   DEFINE DATASET dsSearchCondition FOR ttSearchCondition.

   hdataset = dataset dsSearchCondition:HANDLE.   
           
   DEFINE DATASET dsSoDetail FOR ttsodet.

   hresult = dataset dsSoDetail:HANDLE.    
                

  
mainloop:
repeat on error undo mainloop, leave mainloop:
  
 
   /* Output and Batch Input */
   
   {us/xx/xxworkpath_e.i &outbound=YES &inbound=no &archive=YES}

assign m_outpath     = m_outpath + "so/"
       m_archpath    = m_archpath + "so/"
       m_successpath = m_successpath + "so/"
       m_errorpath   = m_errorpath + "so/"
       m_workpath    = m_workpath + "so/".


   empty temp-table ttsodet.
   
   
   /* Load SEARCH conditions from JSON  */ 
   
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
                    
  if vl_RetOK then do:
   
   for each ttSearchCondition .
   
   if  ttSearchCondition.sonbr_to = ""    then ttSearchCondition.sonbr_to = hi_char.
   if  ttSearchCondition.customer_to = "" then ttSearchCondition.customer_to = hi_char.
   if  ttSearchCondition.site_to = "" then ttSearchCondition.site_to = hi_char.
   
   for each so_mstr no-lock where so_mstr.so_sched = ttSearchCondition.so_sched
                              and so_mstr.so_nbr >= ttSearchCondition.sonbr_from
                              and so_mstr.so_nbr <= ttSearchCondition.sonbr_to 
                              and so_mstr.so_cust >= ttSearchCondition.customer_from
                              and so_mstr.so_cust <= ttSearchCondition.customer_to
                              and so_mstr.so_domain = global_domain,
       each sod_det no-lock where sod_det.sod_nbr = so_nbr 
                              and sod_det.sod_domain = global_domain
							  and sod_det.sod_site >= ttSearchCondition.site_from
							  and sod_det.sod_site <= ttSearchCondition.site_to
							  
                              .
                              
                              
        if so_sched then 
        find scx_ref where scx_order = sod_nbr 
               and scx_line = sod_line
               and scx_type = 1 no-lock no-error.                     
                              
                              
                              
                              
       create ttsodet.
       assign       
           ttsodet.so_nbr            =    so_mstr.so_nbr                   
           ttsodet.so_cust           =    so_mstr.so_cust                
           ttsodet.so_ship           =    so_mstr.so_ship                
           ttsodet.so_ord_date       =    so_mstr.so_ord_date            
           ttsodet.so_req_date       =    so_mstr.so_req_date            
           ttsodet.so_due_date       =    so_mstr.so_due_date            
           ttsodet.so_rmks           =    so_mstr.so_rmks                
           ttsodet.so_cr_terms       =    so_mstr.so_cr_terms            
           ttsodet.so_fob            =    so_mstr.so_fob                 
           ttsodet.so_po             =    so_mstr.so_po                  
           ttsodet.so_shipvia        =    so_mstr.so_shipvia             
           ttsodet.so_pr_list        =    so_mstr.so_pr_list             
           ttsodet.so_xslspsn        =    so_mstr.so_xslspsn             
           ttsodet.so_stat           =    so_mstr.so_stat                
           ttsodet.so_invoiced       =    so_mstr.so_invoiced            
           ttsodet.so_ar_acct        =    so_mstr.so_ar_acct             
           ttsodet.so_ar_cc          =    so_mstr.so_ar_cc               
           ttsodet.so_inv_date       =    so_mstr.so_inv_date            
           ttsodet.so_ship_date      =    so_mstr.so_ship_date           
           ttsodet.so_taxable        =    so_mstr.so_taxable             
           ttsodet.so_curr           =    so_mstr.so_curr                
           ttsodet.so_ex_rate        =    so_mstr.so_ex_rate             
           ttsodet.so_lang           =    so_mstr.so_lang                
           ttsodet.so_type           =    so_mstr.so_type                
           ttsodet.so_site           =    so_mstr.so_site                
           ttsodet.so_taxc           =    so_mstr.so_taxc                
           ttsodet.so_project        =    so_mstr.so_project             
           ttsodet.so_channel        =    so_mstr.so_channel             
           ttsodet.so_fix_rate       =    so_mstr.so_fix_rate            
           ttsodet.so_bill           =    so_mstr.so_bill                
           ttsodet.so_tax_env        =    so_mstr.so_tax_env             
           ttsodet.so_sched          =    so_mstr.so_sched               
           ttsodet.so_pr_list2       =    so_mstr.so_pr_list2            
           ttsodet.so_tax_usage      =    so_mstr.so_tax_usage           
           ttsodet.so_fix_pr         =    so_mstr.so_fix_pr              
           ttsodet.so_ex_rate2       =    so_mstr.so_ex_rate2            
           ttsodet.so_ex_ratetype    =    so_mstr.so_ex_ratetype         
           ttsodet.so_consignment    =    so_mstr.so_consignment         
           ttsodet.so_daybookset     =    so_mstr.so_daybookset                         
           ttsodet.sod_nbr           =    sod_det.sod_nbr                
           ttsodet.sod_due_date      =    sod_det.sod_due_date           
           ttsodet.sod_per_date      =    sod_det.sod_per_date           
           ttsodet.sod_req_date      =    sod_det.sod_req_date           
           ttsodet.sod_line          =    sod_det.sod_line               
           ttsodet.sod_part          =    sod_det.sod_part               
           ttsodet.sod_qty_ord       =    sod_det.sod_qty_ord            
           ttsodet.sod_qty_all       =    sod_det.sod_qty_all            
           ttsodet.sod_qty_pick      =    sod_det.sod_qty_pick           
           ttsodet.sod_qty_ship      =    sod_det.sod_qty_ship           
           ttsodet.sod_qty_inv       =    sod_det.sod_qty_inv            
           ttsodet.sod_loc           =    sod_det.sod_loc                
           ttsodet.sod_type          =    sod_det.sod_type               
           ttsodet.sod_price         =    sod_det.sod_price              
           ttsodet.sod_std_cost      =    sod_det.sod_std_cost           
           ttsodet.sod_qty_chg       =    sod_det.sod_qty_chg            
           ttsodet.sod_bo_chg        =    sod_det.sod_bo_chg             
           ttsodet.sod_acct          =    sod_det.sod_acct               
           ttsodet.sod_taxable       =    sod_det.sod_taxable            
           ttsodet.sod_desc          =    sod_det.sod_desc               
           ttsodet.sod_um            =    sod_det.sod_um                 
           ttsodet.sod_cc            =    sod_det.sod_cc                 
           ttsodet.sod_um_conv       =    sod_det.sod_um_conv            
           ttsodet.sod_project       =    sod_det.sod_project            
           ttsodet.sod_custpart      =    sod_det.sod_custpart           
           ttsodet.sod_status        =    sod_det.sod_status             
           ttsodet.sod_list_pr       =    sod_det.sod_list_pr            
           ttsodet.sod_taxc          =    sod_det.sod_taxc               
           ttsodet.sod_inv_nbr       =    sod_det.sod_inv_nbr            
           ttsodet.sod_site          =    sod_det.sod_site               
           ttsodet.sod_prodline      =    sod_det.sod_prodline           
           ttsodet.sod_tax_in        =    sod_det.sod_tax_in             
           ttsodet.sod_confirm       =    sod_det.sod_confirm            
           ttsodet.sod_ord_mult      =    sod_det.sod_ord_mult           
           ttsodet.sod_pkg_code      =    sod_det.sod_pkg_code           
           ttsodet.sod_sched         =    sod_det.sod_sched              
           ttsodet.sod_sch_data      =    sod_det.sod_sch_data           
           ttsodet.sod_sch_mrp       =    sod_det.sod_sch_mrp            
           ttsodet.sod_translt_days  =    sod_det.sod_translt_days       
           ttsodet.sod_fsm_type      =    sod_det.sod_fsm_type           
           ttsodet.sod_conrep        =    sod_det.sod_conrep             
           ttsodet.sod_start_eff     =    sod_det.sod_start_eff[1]     
           ttsodet.sod_end_eff       =    sod_det.sod_end_eff[1]    
           ttsodet.sod_dock          =    sod_det.sod_dock               
           ttsodet.sod_pr_list       =    sod_det.sod_pr_list            
           ttsodet.sod_translt_hrs   =    sod_det.sod_translt_hrs        
           ttsodet.sod_out_po        =    sod_det.sod_out_po             
           ttsodet.sod_raw_days      =    sod_det.sod_raw_days           
           ttsodet.sod_fab_days      =    sod_det.sod_fab_days           
           ttsodet.sod_tax_usage     =    sod_det.sod_tax_usage          
           ttsodet.sod_rbkt_days     =    sod_det.sod_rbkt_days          
           ttsodet.sod_rbkt_weeks    =    sod_det.sod_rbkt_weeks         
           ttsodet.sod_rbkt_mths     =    sod_det.sod_rbkt_mths          
           ttsodet.sod_auto_ins      =    sod_det.sod_auto_ins           
           ttsodet.sod_tax_env       =    sod_det.sod_tax_env            
           ttsodet.sod_sub           =    sod_det.sod_sub                
           ttsodet.sod_qty_ivcd      =    sod_det.sod_qty_ivcd           
           ttsodet.sod_custref       =    sod_det.sod_custref            
           ttsodet.sod_scx_po        =    scx_ref.scx_po        when avail scx_ref   
           ttsodet.sod_scx_modelyr   =    scx_ref.scx_modelyr   when avail scx_ref         
           ttsodet.sod_consignment   =    sod_det.sod_consignment
           ttsodet.sod_msg         = "SUCCESS"
                 .  


                      
     end.  /*  for each so_mstr no-lock  */                                     
   end.  /*  for each for each ttSearchCondition . */                                     
 
  
 end.  /* if vl_RetOK then do: */ 
                    
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
         vc_topath " " skip 
         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */        
      
   
   
   leave.
end. /* repeat on error undo mainloop, leave mainloop: */
